package com.guigu.commons.security;


import com.guigu.commons.utils.AuthCode;
import com.guigu.commons.utils.ResponseUtil;
import com.guigu.commons.utils.ResultResponseUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName:UnAuthorizedPoint
 * @Description:未授权的方法
 * @Author:longtao
 * @Date:2021/6/28
 * @Version:1.0
 */
public class UnAuthorizedPoint implements AuthenticationEntryPoint {
    /**
     * Commences an authentication scheme.
     * <p>
     * <code>ExceptionTranslationFilter</code> will populate the <code>HttpSession</code>
     * attribute named
     * <code>AbstractAuthenticationProcessingFilter.SPRING_SECURITY_SAVED_REQUEST_KEY</code>
     * with the requested target URL before calling this method.
     * <p>
     * Implementations should modify the headers on the <code>ServletResponse</code> as
     * necessary to commence the authentication process.
     *
     * @param request       that resulted in an <code>AuthenticationException</code>
     * @param response      so that the user agent can begin authentication
     * @param authException that caused the invocation
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        System.out.println("进入了commence方法");
        String uri = request.getRequestURI();
        if (uri.contains("/druid")) {
            System.out.println("uri = " + uri);
        }
        ResponseUtil.out(response, resultResponseUtil());
    }

    private ResultResponseUtil resultResponseUtil() {
        return new ResultResponseUtil(false, "您还未登录", AuthCode.NOT_LOGGEDIN_CODE);
    }
}
